// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import router from './router'
// import apiConfig from '../config/api.config'
import axios from 'axios';
import  VueResource  from 'vue-resource'

import SIdentify from './components/common/identify'
Vue.use(SIdentify)

Vue.use(VueResource);

Vue.use(Vuex)
Vue.config.productionTip = false

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
Vue.prototype.$axios = axios;


Vue.use(ElementUI)



//vuex
const ADD_COUNT = 'ADD_COUNT'; // 用常量代替事件类型，使得代码更清晰
const REMOVE_COUNT = 'REMOVE_COUNT';
const ADD_NICK = 'ADD_NICK';
//注册状态管理全局参数
var store = new Vuex.Store({
  state:{
    token:'',
    nickname:'',
  },
  mutations: {
    //写法与getters相类似
    //组件想要对于vuex 中的数据进行的处理
    //组件中采用this.$store.commit('方法名') 的方式调用，实现充分解耦
    //内部操作必须在此刻完成(同步)
    [ADD_COUNT] (state, token) { // 第一个参数为 state 用于变更状态 登录
      sessionStorage.setItem("token", token);
      state.token = token;
    },
    [REMOVE_COUNT] (state, token) { // 退出登录
 
      sessionStorage.removeItem("token", token);
 
      state.token = token;
    },
    [ADD_NICK] (state, nickname) { // 第一个参数为 state 用于变更状态 登录
      sessionStorage.setItem("nickname", nickname);
      state.nickname = nickname;
    },
  }
});


//界面防跳转
router.beforeEach((to, from, next) => {
  console.log(to);
  console.log(from);
  if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
    if(sessionStorage.getItem('token')){ //判断本地是否存在access_token
      next();
    }else {
      next({
        path:'/'
      })
    }
  }
  else {
    next();
  }
  /*如果本地 存在 token 则 不允许直接跳转到 登录页面*/
  if(to.fullPath == "/"){
    if(sessionStorage.getItem('token')){
      next({
        path:from.fullPath
      });
    }else {
      next();
    }
  }
});


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
